package com.atguigu.flink.charkoer15;


import com.atguigu.flink.been.WaterSensor;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
import org.apache.flink.table.functions.TableFunction;

public class FlinkTableFunction {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 2000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);
        DataStreamSource<WaterSensor> waterSensorStream =
                env.fromElements(
                        new WaterSensor("hello a b abc atguigu", 1000L, 10),
                        new WaterSensor("abc b abc atguigu", 1000L, 10),
                        new WaterSensor("hello  atguigu", 1000L, 10),
                        new WaterSensor("hello  atguigu world", 1000L, 10)
                );


        StreamTableEnvironment tEnv = StreamTableEnvironment.create(env);
        Table table = tEnv.fromDataStream(waterSensorStream);
        tEnv.createTemporaryView("sensor", table);
        tEnv.createTemporaryFunction("my_split", Mysplit.class);
        tEnv.sqlQuery("select " +
                " id," +
                " word," +
                " len" +
                " from sensor" +
                " , lateral table(my_split(id))")
                .execute().print();

//        tEnv
//                .sqlQuery("select " +
//                        " id," +
//                        " word," +
//                        " len " +
//                        "from sensor " +
//                        ", lateral table(my_split(id)) ")
//                .execute()
//                .print();
    }

    public static class Mysplit extends TableFunction<Wolden> {
        public void eval(String a) {
            for (String s : a.split(" ")) {
                collect(new Wolden(s, s.length()));
            }
        }
    }
}
